



//
//  NMyFeedbackViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/20.
//  Copyright © 2018年 youedata. All rights reserved.
// 我的反馈

#import "NMyFeedbackViewController.h"
#import "NProblemContentViewController.h"
#import "NFeedbackCell.h"
#import "NFeedbackModel.h"
#import "YearMonthPickerView.h"
#import "NFilterView.h"
#import "ITDatePickerController.h"
@interface NMyFeedbackViewController ()
<UITableViewDelegate,UITableViewDataSource,STPickerSingleDelegate,YearMonthPickerViewDelegate,NFilterViewDelegate,ITDatePickerControllerDelegate>

@property (nonatomic,strong)STPickerSingle *picker;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *picArray;
@property (nonatomic,strong)NSArray *titles;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger indexFlag;

@property (nonatomic,strong)NFilterView *filterView;

///筛选时间
@property (nonatomic,copy)NSString *filterTime;
///筛选类型
@property (nonatomic,copy)NSString *filterType;
///筛选状态
@property (nonatomic,copy)NSString *filterStatus;


@property (nonatomic,assign)NSInteger currentPage;


@end

#define PageSize 20

#define cellIdentifier @"feedbackid"
#define SectionHeaderHeight  15.f

@implementation NMyFeedbackViewController


-(UITableView *)tableView{
    
    WeakStament(ws);
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64-50) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NFeedbackCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [ws loadData];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [ws loadMoreData];
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = kWhiteColor;
    
    self.dataArray = [NSMutableArray array];
    self.currentPage = 0;
    self.indexFlag = -1;
    
    [NUserModel shareInstance].index = -1;
    
    if ([self.flag isEqualToString:@"open"]) {
        
         [self.navigationItem setItemWithTitle:@"公开反馈" textColor:kNavigationTitleColor fontSize:18 itemType:center];
        self.indexFlag = 0;
//        [self loadPulicQuestion];
        
        
    }else if([self.flag isEqualToString:@"like"]){
        
        [self.navigationItem setItemWithTitle:@"我的收藏" textColor:kNavigationTitleColor fontSize:18 itemType:center];
        self.indexFlag = 2;
//        [self loadCollectionData];
        
    }else{
        
         [self.navigationItem setItemWithTitle:@"我的反馈" textColor:kNavigationTitleColor fontSize:18 itemType:center];
        //我的反馈
//         [self loadMyFeedbackData];
        self.indexFlag = 1;
    }
    [self.view addSubview:self.tableView];
    
    [self addBottomFilterView];
    // Do any additional setup after loading the view.
}


///添加底部筛选
- (void)addBottomFilterView {
    
    WeakStament(ws);
    NFilterView *filter = [[NFilterView alloc]initWithFrame:CGRectMake(0, ScreenHeight-64-50, ScreenWidth, 50)];
    filter.timeActionBolck = ^(UIButton * _Nonnull btn) {
        //添加年月选择器
        [ws yearMonthPicker];
    };
    filter.delegate = self;
    self.filterView = filter;
    [self.view addSubview:filter];
}

#pragma mark - filterdelegte

- (void)filterPickerSingle:(STPickerSingle *)picker selectedTitle:(NSString *)selectedTitle selectIndex:(NSInteger)selectIndex{
    
    YYLog(@"%@",selectedTitle);
    
    if (picker.tag==111) {
        ///类型
        self.filterType = [NSString stringWithFormat:@"%li",selectIndex+1];
        YYLog(@"%@",self.filterType);
        [self.filterView.midBtn setTitle:[NSString stringWithFormat:@"类型：%@",selectedTitle] forState:UIControlStateNormal];
        
    }else{
        
        ///状态
        self.filterStatus = [NSString stringWithFormat:@"%li",selectIndex];
        YYLog(@"%@",self.filterStatus);
        [self.filterView.rightBtn setTitle:[NSString stringWithFormat:@"状态：%@",selectedTitle] forState:UIControlStateNormal];
    }
    
    [self loadData];
}

-(void)filterYearMonthPickerViewDidClickDone:(YearMonthPickerView *)yearMonthPickerView{
    
    YYLog(@"%@,%@",yearMonthPickerView.selectedMonth,yearMonthPickerView.selectedYear);
    //    self.filterTime = [NSString stringWithFormat:@"%@-%@",yearMonthPickerView.selectedYear,yearMonthPickerView.selectedMonth];
    //
    //    [self loadData];
    
}

- (void)yearMonthPicker {
    
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 630;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];

}


#pragma mark - 年月选择器
- (void)datePickerController:(ITDatePickerController *)datePickerController didSelectedDate:(NSDate *)date dateString:(NSString *)dateString{
    ///tag值真正应用起来，比如那种需要输入很多资料的资料框，每个市一行cell ，就可以用indexpath.row作为tag值就会比较方便
    if (datePickerController.tag==630) {
        YYLog(@"%@",dateString);
        NSMutableString *mut = [[NSMutableString alloc]initWithString:dateString];
        ///拼接成后台需要的数据格式
        [mut insertString:@"-" atIndex:4];
        self.filterTime = (NSString *)mut;
        
        [self.filterView.leftBtn setTitle:[NSString stringWithFormat:@"时间：%@",mut] forState:UIControlStateNormal];
        
        [self loadData];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)loadData {
    
     [SVProgressHUD show];
    self.currentPage = 1;
    
    if (self.indexFlag==0) {
        [self loadPulicQuestion];
    }else if (self.indexFlag==2){
        [self loadCollectionData];
    }else{
        [self loadMyFeedbackData];
    }
    
}

- (void)loadMoreData{
    self.currentPage++;
    if (self.indexFlag==0) {
        [self loadPulicQuestion];
    }else if (self.indexFlag==2){
        [self loadCollectionData];
    }else{
        [self loadMyFeedbackData];
    }
}

- (void)formData:(NSArray *)data {
    
    if (self.currentPage==1) {
         [self.dataArray removeAllObjects];
    }
    for (NSDictionary *dict in data) {
        NFeedbackModel *model = [NFeedbackModel mj_objectWithKeyValues:dict];
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
    [self endRefresh];
    
}


///没有更多数据
- (void)noMoreData {
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}


#pragma mark -公开问题列表
- (void)loadPulicQuestion {
    
    WeakStament(ws);
    NSDictionary *dic = @{@"questionCreateTimeBegin":self.filterTime?self.filterTime:@"",@"questionType":self.filterType?self.filterType:@"",@"questionProgress":self.filterStatus?self.filterStatus:@"",@"size":@(kPageSize),@"curPage":@(self.currentPage)};
    [[NNRCNetRequest manager]getWithUrl:kEnterPrisePulicQuestionUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"code"] isEqualToString:@"0"]) {
            NSArray *arr = result[@"page"][@"records"];
            [ws formData:arr];
            if (arr.count==0) {
                [ws noMoreData];
            }
            [SVProgressHUD dismiss];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
        [self endRefresh];
         [SVProgressHUD dismiss];
    }];

}
#pragma mark -我的收藏
- (void)loadCollectionData {
    WeakStament(ws);
    NSDictionary *dic = @{@"userId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@"",@"questionCreateTimeBegin":self.filterTime?self.filterTime:@"",@"questionType":self.filterType?self.filterType:@"",@"questionProgress":self.filterStatus?self.filterStatus:@"",@"size":@(kPageSize),@"curPage":@(self.currentPage)};
    [[NNRCNetRequest manager]getWithUrl:kCollectionListUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"code"] isEqualToString:@"0"]) {
            NSArray *arr = result[@"page"][@"records"];
            [ws formData:arr];
            if (arr.count==0) {
                [ws noMoreData];
            }
             [SVProgressHUD dismiss];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
         [self endRefresh];
         [SVProgressHUD dismiss];
    }];
}

#pragma mark - 我的反馈
///我的反馈
- (void)loadMyFeedbackData{
    
    WeakStament(ws);
    NSDictionary *dic = @{@"userId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@"",@"questionCreateTimeBegin":self.filterTime?:@"",@"questionType":self.filterType?:@"",@"questionProgress":self.filterStatus?:@"",@"size":@(kPageSize),@"curPage":@(self.currentPage)};
    [[NNRCNetRequest manager]getWithUrl:kEnterPriseFeedbackQuestionListUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"code"] isEqualToString:@"0"]) {
            NSArray *arr = result[@"page"][@"records"];
            [ws formData:arr];
            if (arr.count==0) {
                [ws noMoreData];
            }
             [SVProgressHUD dismiss];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
         [SVProgressHUD dismiss];
    }];
    
}

- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NFeedbackCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NFeedbackModel *model = self.dataArray[indexPath.row];
    
    if (self.dataArray.count>indexPath.row) {
        [cell configWithData:model];
        YYLog(@"%@",cell.model);
    }
    return cell;
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NFeedbackModel *model = self.dataArray[indexPath.row];
    NProblemContentViewController *pro = [NProblemContentViewController new];
    if ([self.flag isEqualToString:@"open"]||[self.flag isEqualToString:@"like"]) {
        pro.pushType = NProblemPushTypeCollection;
    }
    pro.hidesBottomBarWhenPushed = YES;
    pro.problemID = [NSString stringWithFormat:@"%ld",(long)model.questionId];
    [self.navigationController pushViewController:pro animated:YES];
}
@end
